package com.cgc.venus.server.job.controller;
import com.cgc.venus.server.core.bean.response.VenusResponse;
import com.cgc.venus.server.core.bean.response.VenusResponseStatus;
import com.cgc.venus.server.core.exception.VenusException;
import com.cgc.venus.server.job.inter.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * Created by 业哥 on 2017/11/8.
 */
@Api(value = "任务", description = "任务管理服务的api")
@RequestMapping("/job-admin")
@RestController
public class JobController {
    private static final Logger logger = LoggerFactory.getLogger(JobController.class);
    @Autowired
    private JobService jobServiceImpl;
    @ApiOperation("创建任务")
    @RequestMapping(value = "/job", method = RequestMethod.POST)
    @ResponseBody
    public VenusResponse<Boolean> addJob(@RequestParam(value="jobName")String jobName,
                                          @RequestParam(value="jobClassName")String jobClassName,
                                          @RequestParam(value="jobGroupName")String jobGroupName,
                                          @RequestParam(value="cronExpression")String cronExpression,
                                          @RequestParam(value="desc")String desc) throws VenusException{
        jobServiceImpl.addJob(jobName,jobClassName,jobGroupName,cronExpression,desc);
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"创建任务成功",true);
        return response;
    }

    @ApiOperation("暂停任务")
    @RequestMapping(value = "/job/pause", method = RequestMethod.POST)
    @ResponseBody
    public VenusResponse<Boolean> pauseJob(@RequestParam(value="jobName")String jobName,
                                         @RequestParam(value="jobGroupName")String jobGroupName) throws VenusException{
        jobServiceImpl.pauseJob(jobName,jobGroupName);
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"暂停任务成功",true);
        return response;
    }

    @ApiOperation("重启任务")
    @RequestMapping(value = "/job/resume", method = RequestMethod.POST)
    @ResponseBody
    public VenusResponse<Boolean> resumeJob(@RequestParam(value="jobName")String jobName,
                                           @RequestParam(value="jobGroupName")String jobGroupName) throws VenusException{
        jobServiceImpl.resumeJob(jobName, jobGroupName);
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"重启任务成功",true);
        return response;
    }

    @ApiOperation("重新设置任务")
    @RequestMapping(value = "/job/reschedule", method = RequestMethod.POST)
    @ResponseBody
    public VenusResponse<Boolean> rescheduleJob(@RequestParam(value="jobName")String jobName,
                                            @RequestParam(value="jobGroupName")String jobGroupName,
                                            @RequestParam(value="cronExpression")String cronExpression,
                                            @RequestParam(value="desc")String desc) throws VenusException{
        jobServiceImpl.rescheduleJob(jobName, jobGroupName,cronExpression,desc);
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"重新设置成功",true);
        return response;
    }

    @ApiOperation("删除任务")
    @RequestMapping(value = "/job", method = RequestMethod.DELETE)
    @ResponseBody
    public VenusResponse<Boolean> deleteJob(@RequestParam(value="jobName")String jobName,
                                                @RequestParam(value="jobGroupName")String jobGroupName) throws VenusException{
        jobServiceImpl.deleteJob(jobName, jobGroupName);
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"删除成功",true);
        return response;
    }

    @ApiOperation("查询任务")
    @RequestMapping(value = "/job", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<List> queryJob(@RequestParam(value="queryKey")String queryKey
            ,@RequestParam(value="status")String status) throws VenusException{
        List list =jobServiceImpl.queryJob(queryKey,status);
        VenusResponse<List> response=new VenusResponse<List>(VenusResponseStatus.OK,"",list);
        return response;
    }

    @ApiOperation("校验任务是否重复")
    @RequestMapping(value = "/job/validate", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<Boolean> validateJob(@RequestParam(value="jobName")String jobName,
                                           @RequestParam(value="jobGroupName")String jobGroupName) throws VenusException{
        boolean f= jobServiceImpl.checkExistsJobKey(jobName,jobGroupName);
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"",f);
        return response;
    }
    @ApiOperation("校验是否有类名")
    @RequestMapping(value = "/job/validateClass", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<Boolean> checkExistsClass(@RequestParam(value="className")String className) throws VenusException{
        boolean f= jobServiceImpl.checkExistsClass(className);
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"",f);
        return response;
    }

    @ApiOperation("校验corn表达式")
    @RequestMapping(value = "/job/validateCornExpression", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<Boolean> checkExistsCornExpression(@RequestParam(value="cornExpression")String cornExpression) throws VenusException{
        boolean f= jobServiceImpl.checkExistsCornExpression(cornExpression);
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"",f);
        return response;
    }
}